package com.jpx.jxcbiz.web;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jpx.common.dto.TGoodsDto;
import com.jpx.jxc.common.vo.ResultEntity;
import com.jpx.common.vo.GoodsDto;
import com.jpx.jxcbiz.entity.TGoods;
import com.jpx.jxcbiz.service.ITGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.web.multipart.MultipartFile;


import java.io.ByteArrayOutputStream;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Mht
 * @since 2020-02-24
 */
@RestController
@RequestMapping("/biz/tGoods")
@Slf4j
public class TGoodsController {

    @Resource
    private ITGoodsService itGoodsService;

    /**
     * 导出表格
     * @param goodsDto
     * @return
     * @throws Exception
     */
    @RequestMapping("/downLoadGoodsExcel")
    public ResponseEntity downLoadGoodsExcel(GoodsDto goodsDto) throws Exception{
        XSSFWorkbook xSSFWorkbooks = itGoodsService.downLoadGoodsExcel(goodsDto);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        xSSFWorkbooks.write(byteArrayOutputStream);

        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.setContentDispositionFormData("attachment", new String("商品库存表.xlsx".getBytes("UTF-8"), "ISO-8859-1"));
        httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity(byteArrayOutputStream.toByteArray(),httpHeaders, HttpStatus.CREATED);
    }


    List<String> excelname = Arrays.asList(".xls",".xlsx");

    /**
     * 导入表格
     * @param file
     * @return
     * @throws Exception
     */
    @RequestMapping("/upLoadExcel")
    public ResultEntity  upLoadExcel(MultipartFile file) throws Exception {
        if (file == null){
            throw new Exception("文件为空");
        }
        String fileType = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));

        if (!excelname.contains(fileType)){
            throw new Exception("只能导入excel格式的文件");
        }
        try {
            this.itGoodsService.uplodExcel(file);
        }catch (Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            throw new Exception("导入数据失败");
        }
        return ResultEntity.ok("导入数据成功");
    }

    /**
     * 查询所有库存
     * 根据商品名称，类别，编码 模糊查询 参数name(商品名称，商品参数)，bname(商品类别)
     * @return
     */
    @RequestMapping("/getGoodsList")
    public ResultEntity getGoodsList(GoodsDto goodsDto){
        PageHelper.startPage(goodsDto.getPageNo(),goodsDto.getPageSize());
        Wrapper wrapper = new EntityWrapper();
        if(goodsDto.getName()!=null && goodsDto.getName()!=""){
            wrapper.eq("code",goodsDto.getName()).or().like("name",goodsDto.getName());
        }
        if(goodsDto.getBname()!=null && goodsDto.getBname()!=""){
            wrapper.eq("bname",goodsDto.getBname());
        }
        if(goodsDto.getTypeId()!=null){
            wrapper.eq("type_id",goodsDto.getTypeId());
        }

        List goodsList = itGoodsService.getGoodsList(goodsDto.getName(),goodsDto.getBname(),goodsDto.getTypeId());

        PageInfo goodsDtoPageInfo = new PageInfo(goodsList);
        return ResultEntity.ok(goodsDtoPageInfo);
    }

    @Resource
    ITGoodsService goodsService;

    @RequestMapping("/list")
    public ResultEntity list(TGoodsDto goodsDto){
        PageHelper.startPage(goodsDto.getPageNo(),goodsDto.getPageSize());
        List<TGoodsDto> list = goodsService.list(goodsDto);

        PageInfo<TGoodsDto> pageInfo = new PageInfo<>(list);
        return ResultEntity.ok(pageInfo);
    }

    @RequestMapping("/insert")
    public ResultEntity insert(TGoodsDto goodsDto){
        TGoods goods = new TGoods();
        BeanUtils.copyProperties(goodsDto,goods);
        boolean b = goodsService.insert(goods);
        if(b){
            return ResultEntity.ok(1);
        }
        return ResultEntity.ok(0);
    }

    @RequestMapping("/update")
    public ResultEntity update(TGoodsDto goodsDto){
        TGoods goods = new TGoods();
        BeanUtils.copyProperties(goodsDto,goods);
        boolean b = goodsService.updateById(goods);
        if(b){
            return ResultEntity.ok(1);
        }
        return ResultEntity.ok(0);
    }

    @RequestMapping("/delete")
    public ResultEntity update(int id){
        boolean b = goodsService.deleteById(id);
        if(b){
            return ResultEntity.ok(1);
        }
        return ResultEntity.ok(0);
    }

    @RequestMapping("/goodList")
    public ResultEntity goodList(TGoodsDto goodsDto){
        PageHelper.startPage(goodsDto.getPageNo(),goodsDto.getPageSize());
        List<TGoods> list = goodsService.goodList(goodsDto);
        PageInfo<TGoods> pageInfo = new PageInfo<>(list);
        return ResultEntity.ok(pageInfo);
    }

    @RequestMapping("/repertoryList")
    public ResultEntity repertoryList(TGoodsDto goodsDto){
        PageHelper.startPage(goodsDto.getPageNo(),goodsDto.getPageSize());
        TGoods goods = new TGoods();
        EntityWrapper<TGoods> entityWrapper = new EntityWrapper<>(goods);
        entityWrapper.notIn("inventory_quantity",0);
        List<TGoods> list = goodsService.selectList(entityWrapper);
        PageInfo<TGoods> pageInfo = new PageInfo<>(list);
        return ResultEntity.ok(pageInfo);
    }

    @RequestMapping("/addRepertory")
    public ResultEntity addRepertory(TGoodsDto goodsDto){
        TGoods goods = new TGoods();
        BeanUtils.copyProperties(goodsDto,goods);
        boolean b = goodsService.updateById(goods);
        if(b){
            return ResultEntity.ok(1);
        }
        return ResultEntity.ok(0);
    }

    @RequestMapping("/delRepertory")
    public ResultEntity delRepertory(TGoodsDto goodsDto){
        TGoods goods = new TGoods();
        BeanUtils.copyProperties(goodsDto,goods);
        goods.setInventoryQuantity(0);
        boolean b = goodsService.updateById(goods);
        if(b){
            return ResultEntity.ok(1);
        }
        return ResultEntity.ok(0);
    }

    @RequestMapping("/updRepertory")
    public ResultEntity updRepertory(TGoodsDto goodsDto){
        TGoods goods = new TGoods();
        BeanUtils.copyProperties(goodsDto,goods);
        boolean b = goodsService.updateById(goods);
        if(b){
            return ResultEntity.ok(1);
        }
        return ResultEntity.ok(0);
    }

}
